import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { AIContentDocumentType } from 'src/schema/User/ai.schema';

@Injectable()
export class AiService {
  constructor(
    @InjectModel('AIContent')
    private readonly aiContentModel: Model<AIContentDocumentType>,
  ) {}

  async createContent(body: any) {
    const createAIContent = new this.aiContentModel(body);
    createAIContent.save();
    return {
      code: 200,
      msg: '上传成功',
    };
  }

  async getContent(query) {
    const { user_id, _id } = query;

    if (_id) {
      const result = await this.aiContentModel.findById(_id);

      return {
        code: 200,
        data: result,
      };
    } else {
      const result = await this.aiContentModel.find({ user_id });
      return {
        code: 200,
        data: result,
      };
    }
  }

  //修改
  async updateContent(id: string, body: any) {
    console.log(id);
    
    const result = await this.aiContentModel.findByIdAndUpdate(id, body, {
      new: true,
    });
    return {
      code: 200,
      msg: '修改成功',
      data: result,
    };
  }
}
